package com.enjoy.book.biz;

import com.enjoy.book.bean.*;
import com.enjoy.book.dao.BookData;
import com.enjoy.book.dao.RecordData;


import java.sql.SQLException;
import java.util.List;

/**
 * @author 陈俊威
 */

public class BookBiz {
    /**BookDao的对象
     *
     */
    BookData bookData = new BookData();
    public int add(String name, double price, String desc,
                   String publish, String author, long stock, String address)  {
        int count = 0;
        try {
            count =  bookData.add(name,price,desc,publish,author,stock,address);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;

    }
    public int add(Book book){
        return add(book.getName(),book.getPrice(),book.getDesc(),book.getPublish(),book.getAuthor(),book.getStock(),book.getAddress());
    }

    public int modify(long id, long typeId, String name, double price, String desc, String pic,
                      String publish, String author, long stock, String address)  {
        int count = 0;
        try {
            count = bookData.modify(id,name,price,desc,publish,author,stock,address);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }
    public int modify(Book book)  {
        int count = 0;
        try {
            count = bookData.modify(book.getId(),book.getName(),book.getPrice(),book.getDesc(),book.getPublish(),book.getAuthor(),book.getStock(),book.getAddress());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    public int remove(long id) throws Exception {
        RecordData recordData = new RecordData();
        int count = 0;
        try {
            //1.判断id是否存在外键
            List<Record> records = recordData.getRecordByBookId(id);
            if(records.size()>0){
                throw new Exception("删除的书籍有子信息，删除失败");
            }
            //2.删除
            count = bookData.remove(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;

    }

    public List<Book>  getByPage(int pageIndex,int pageSize) {
      //  TypeDao typeDao = new TypeDao();
        List<Book> books = null;
        try {
            books = bookData.getByPage(pageIndex,pageSize);
            /**处理type对象的数据问题
             *
             */

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return books;

    }
    public Book getByName(String name) {
        Book book = null;
        //   TypeDao typeDao = new TypeDao();

        try {
            book = bookData.getByName(name);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return book;
    }

    public Book getById(long id) {
        Book book = null;
     //   TypeDao typeDao = new TypeDao();

        try {
            book = bookData.getById(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return null;
        }
        return book;
    }

    /**
     * 由行数算页数
     * @return
     */
    public int  getPageCount(int pageSize) {
        int pageCount = 0;
        try {
            //1.获取行数
            int rowCount = bookData.getCount();
            //2.根据行数得到页数,每页多少条
            pageCount =  (rowCount-1)/pageSize+1;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return pageCount;
    }
}

